#include <LiquidCrystal.h>
#include <OneWire.h>



// DS18S20 Temperature chip i/o
OneWire ds(7);  // on pin 10
byte addr[8];
byte data;
unsigned long nilai;
float suhu;
LiquidCrystal lcd = LiquidCrystal(A0,A1,A2,5,4,3,2);
byte newChar[8] = {
	B01100,
	B10010,
	B10010,
	B01100,
	B00000,
	B00000,
	B00000,
	B00000
};
void setup(){
  lcd.begin(8,2);
  lcd.createChar(7,newChar);
  while ( !ds.search(addr)) { 
  }
  //Serial.print("No more addresses.\n");
  ds.reset_search();
  //return;

  //DDR_SPI = (1<<DD_MOSI)|(1<<DD_SCK);
  /* Enable SPI, Master, set clock rate fck/16 */

  pinMode(10,OUTPUT);//SS
  pinMode(11,OUTPUT);//MOSI
  pinMode(12,INPUT);//MISO
  pinMode(13,OUTPUT);//SCK
  //pinMode(7,OUTPUT);
  digitalWrite(7,LOW);//reset optional

  SPCR = (1<<SPE)|(1<<MSTR)|(0<<SPR0)|(0<<SPR1);//|(1<<CPOL);//|(1<<CPHA);
  data = SPSR;
  data = SPDR;
  Serial.begin(9600);
  //delay(1);
  Serial.println("Online");
  //digitalWrite(7,HIGH);
  //delay(2000);
  //init si maksim
  //data = DRDY | RS2 | RS1 | RS0 | R/W' | PD | CH1 | Ch0
  //setting clock register
  data = sendSPI(0x20);//write clock reg
  Serial.println(data,HEX);
  data = sendSPI(0b10100100);//internal clock,50Hz filter
  Serial.println(data,HEX);
  //verify clock register
  data = sendSPI(0b00101000);//read clock reg
  Serial.println(data,HEX);
  data = sendSPI(0xF0);
  Serial.print("clock reg =");
  Serial.println(data,HEX);
  //setting setup register
  data = sendSPI(0x10);
  Serial.println(data,HEX);
  data = sendSPI(0b01011110);//start calib+conv,
  Serial.println(data,HEX);
  Serial.println("Start");


}

void loop(){
  int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract;

  byte i;
  byte present = 0;
  byte sdata[12];


  do{
    data = sendSPI(0x08);//baca comm register
    data = sendSPI(0x00);
  }
  while(data & (0b10000000));
  data = sendSPI(0x38);
  data = sendSPI(0x00);
  nilai = ((unsigned int) data) << 8;
  data = sendSPI(0x00);
  nilai |= data;
  //Serial.print(nilai,DEC);
  //Serial.print(";");
  //Serial.println(nilai,BIN);
  suhu = (nilai * 300ul) / 65535e0;
  //Serial.print(';');
  //Serial.print(suhu,DEC);
  suhu *= -23.1e-9;
  suhu += 17.58480889e-6;
  suhu = sqrt(suhu);
  suhu += -3.9083e-3;
  suhu /= -1.155e-6;
  //Serial.print(';');
  //Serial.print(suhu,DEC);
  //Serial.println("C");
  



  //------------------------------------------------------------
  //OneWire

  if ( OneWire::crc8( addr, 7) != addr[7]) {
    Serial.print("CRC is not valid!\n");
    return;
  }
  ds.reset();
  ds.select(addr);
  ds.write(0x44,1);         // start conversion, with parasite power on at the end
  delay(1000);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.
  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad
  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    sdata[i] = ds.read();
  }
  LowByte = sdata[0];
  HighByte = sdata[1];
  TReading = (HighByte << 8) + LowByte;
  SignBit = TReading & 0x8000;  // test most sig bit

  if (SignBit) // negative
  {
    TReading = (TReading ^ 0xffff) + 1; // 2's comp
  }
  Tc_100 = (6 * TReading) + TReading / 4;    // multiply by (100 * 0.0625) or 6.25
  Whole = Tc_100 / 100;  // separate off the whole and fractional portions
  Fract = Tc_100 % 100;
  lcd.clear();
  //lcd.setCursor(0,1);
  lcd.print("Suhu = ");
  lcd.print(suhu,3);
  lcd.print((char)223);
  lcd.print("C");
  lcd.setCursor(0,1);

  if (SignBit) // If its negative
  {
    lcd.print("-");
  }
  lcd.print(Whole);
  lcd.print(".");
  if (Fract < 10)
  {
    lcd.print("0");
  }
  lcd.print(Fract);
  lcd.print((char) 15);
  lcd.write('C');
  //Serial.print("\n");
}

void spi_wait() {
  do {
  } 
  while (!(SPSR & (1 << SPIF)));
}

uint8_t sendSPI(volatile uint8_t fur){
  uint8_t ret;
  SPDR = fur;
  spi_wait();
  ret = SPDR;//clear
  return ret;
}



